﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using VideoClassLibrary.Model;
using VideoClassLibrary.Extension;

namespace VideoClassLibrary.BusinessLogicLayer
{
    public class UserControlModule
    {
        public HttpContext Context { get; private set; }
        public string PostData { get; set; }

        public string StrValue { get; set; }
        UserModule um = new UserModule();
        public UserControlModule(HttpContext context)
        {
            Context = context;

            if (Context.Request.Form["strValue"] != null)
                StrValue = Context.Request.Form["strValue"];
            if (Context.Request.Form["PostData"] != null)
                PostData = Context.Request.Form["PostData"];
        }


        /// <summary>
        /// 判断用户名是否已经存在
        /// </summary>
        /// <returns></returns>
        public string CheckUserExit()
        {

            return um.IfNameExist(StrValue).ToString();

        }

        /// <summary>
        /// 用户异步登录
        /// </summary>
        /// <returns></returns>
        public string UserLogin() {
            string rtnMessage;
            string schoolId = Context.Request["schoolID"];

            var sm = new UserModule();
            V_user user = sm.UserLoginBySchoolID(PostData, StrValue, Convert.ToInt32(schoolId));

            if (user != null)
            {
                if (user.IsAvailable)
                {
                    sm.AddUserLog(user);
                    HttpContext.Current.Session["currentUser"] = user;
                    rtnMessage = "Success";
                }
                else
                {
                    rtnMessage = "用户已被禁用，请联系管理员！！";
                }
            }
            else {

                rtnMessage = "登录名或密码错误！";
            }
            return rtnMessage;
          
        
        }


        /// <summary>
        /// 用户密码找回
        /// </summary>
        /// <returns></returns>
        public string FindUserPwd()
        {

            if (HttpContext.Current.Session["CheckCode"] != null)
            {
                if (HttpContext.Current.Session["CheckCode"].ToString() != StrValue)
                    return "ErrorVCode";
            }
            //首先验证用户名和邮箱是否一致
            var user = PostData.FromJsonTo<V_user>();
            if (um.CheckUnameByEmail(user))
            {
                //发送邮件到用户邮箱
                var eh = new EmailHelper();
                var random = new Random();
                var newPwd = random.Next(0, 999999).ToString();
                if (eh.Send(user.VU_email, "密码找回", "尊敬的学员你好：你的新密码是" + newPwd))
                {
                    //邮件发送成功了 保存用户的新密码
                    user.VU_Password = newPwd;
                    if (um.UpdateUserPwd(user) > 0)
                    {
                        return "True";
                    }
                }

            }
            return "False";
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <returns></returns>
        public string RegistUser()
        {
            var user = PostData.FromJsonTo<V_user>();
            user.create_time = DateTime.Now;
            user.IsAvailable = false;
            user.update_time = DateTime.Now;
            user.IsBuyingAccount = 1;
            user.VU_Password = StringUtils.EncryptPassWord(user.VU_Password,StringUtils.PassFormat.MD5);
            if (um.AddUser(user) > 0)
            {
                //记录用户登录日志
                um.AddUserLog(user);
                //将用户登录
                HttpContext.Current.Session["currentUser"] = user;
                return "1";
            }
            return "0";
        }


    }
}
